angular.module('loginservices', [])

  .factory('Member', function($resource,$rootScope,ENV,Storage,Toast) {
    //访问的地址
    var APIUrl = ENV.api;
    //接收返回值
    var  loginJson,registerJson,procotolJson,resetPwdJson,memberInfoJson,verifyCodeJson,adImgJson;
    var qqLoginInfo;
    var weiBoLoginInfo;
    var weixinLoginInfo;
    var firstInLogin = true;



    //存储的key
    //获取$resource
    var loginResource = $resource(APIUrl+'memberLogin/login.do');
    var registerResource = $resource(APIUrl+'memberLogin/register.do');
    var procotolResource = $resource(APIUrl+'memberLogin/getServiceAgree.do');
    var resetPwdResource = $resource(APIUrl+'memberLogin/resetPwd.do');
    var getmemberInfoResource = $resource(APIUrl+'meInfo/getmemberInfo.do');
    var getVerifyCodeResource = $resource(APIUrl+'memberLogin/getVerifyCode.do');

    var qqLoginResource = $resource(APIUrl+'memberLogin/qqLogin.do');
    var weiBoLoginResource = $resource(APIUrl+'memberLogin/weiBoLogin.do');
    var weixinLoginResource = $resource(APIUrl+'memberLogin/weixinLogin.do');
    var AdResource = $resource(APIUrl+'meMore/findAdImg.do');

    return {
      setFirstInLogin:function(){
        firstInLogin = false;
      },
      getIsFirstInLogin:function(){
        return firstInLogin;
      },
      //获取广告页
      findAdImg:function(){
        AdResource.save({
          member_id:1
        },function(response){
          adImgJson = response;
          $rootScope.$broadcast('Member.AdImgUpdated');
        });
      },
      getAdImgJson:function(){
        return adImgJson;
      },
      //根据用户的编号获取到会员的信息
      getmemberInfo:function(memberId){
        getmemberInfoResource.save({
          member_id:memberId
        },function(response){
          memberInfoJson = response;
          $rootScope.$broadcast('Member.MemberInfoUpdated');
        });
      },
      getMemberInfoJson:function(){
        return memberInfoJson
      },
      //登录
      login: function(username,password){
         loginResource.save({
          member_phone: username,
          member_password: password
        }, function(response) {
          //获取到返回的信息
          loginJson=response;
          $rootScope.$broadcast('Member.loginUpdated');
        });
      },
      //注册
      register:function(username,password,repassword,verifyCode){
          registerResource.save({
           member_phone:username,
           member_password:password,
           repassword:repassword,
           verifyCode:verifyCode
         },function(response){
           registerJson = response;
           $rootScope.$broadcast('Member.registerUpdated');
         })
      },
      //忘记密码
      resetPwd:function(username,password,repassword,verifyCode){
         resetPwdResource.save({
          member_phone:username,
          member_password:password,
          repassword:repassword,
          verifyCode:verifyCode
        },function(response){
          resetPwdJson = response;
          $rootScope.$broadcast('Member.resetPwdUpdated');
        })
      },
      //退出登录
      logout: function() {
        Storage.remove('member');
        Storage.remove('memberLoginInfo');
      },
      //获取服务协议
      getProcotol:function(){
         procotolResource.get(function(response){
         procotolJson = response;
         $rootScope.$broadcast('Member.procotolUpdated');
       });
      },
      //获取到登录的用户信息
      getLoginInfo: function(){
        return loginJson;
      },
      //获取到注册返回的信息
      getRegisterInfo:function(){
        return registerJson;
      },
      //获取服务协议
      getProcotolInfo:function(){
        return procotolJson;
      },
      //获取忘记密码放回的信息
      getResetPwdInfo:function(){
        return resetPwdJson;
      },
      //请求后台获取验证码
      getVerifyCode : function(phoneNum){
        getVerifyCodeResource.save(
          {
            phoneNum : phoneNum
          },
          function (data){
            verifyCodeJson = data;
            $rootScope.$broadcast("Member.getVerifyCode");
          }
        );
      },
      //获取验证码信息
      getVerifyCodeInfo : function (){
        return verifyCodeJson;
      },

      qqLogin : function (userid,access_token){
        qqLoginResource.save(
          {
            userid : userid,
            access_token : access_token
          },
          function (data){
            qqLoginInfo = data;
            $rootScope.$broadcast("Member.qqLogin");
          }
        );
      },

      getQqLoginInfo : function () {
        return qqLoginInfo;
      },

      weiBoLogin : function (userid,access_token){
        weiBoLoginResource.save(
          {
            userid : userid,
            access_token : access_token
          },
          function (data){
            weiBoLoginInfo = data;
            $rootScope.$broadcast("Member.weiBoLogin");
          }
        );
      },

      getWeiBoLoginInfo : function () {
        return weiBoLoginInfo;
      },

      //微信登录
      weixinLogin : function (openid){
        weixinLoginResource.save(
          {
            openid : openid
          },
          function (data) {
            weixinLoginInfo = data;
            $rootScope.$broadcast('Member.weixinLogin');
          }
        )
      },

      getWeixinLoginInfo : function () {
        return weixinLoginInfo;
      }

      /*//检查手机是否安装了QQ
      checkQqIsInstalled : function(){
        YCQQ.checkClientInstalled(function(){
          isQqInstalled = true;
        },function(){
          isQqInstalled = false;
        });
      },
      getIsQqInstalled : function () {
        return isQqInstalled;
      },
      //检查手机是否安装了微博
      checkWeiBoIsInstalled : function(){
        YCWeibo.checkClientInstalled(function(){
          isWeiBoInstalled = true;
        },function(){
          isWeiBoInstalled = false;
        });
      },
      getIsWeiBoInstalled : function () {
        return isWeiBoInstalled;
      },
      //检查手机是否安装了微信
      checkWechatIsInstalled : function(){
          Wechat.isInstalled(function (installed) {
            if(installed == 1) {
              isWechatInstalled = true;
            }else{
              isWechatInstalled = false;
            }
          }, function (reason) {
            isWechatInstalled = false;
        });
      },
      getIsWeChatInstalled : function () {
        return isWechatInstalled;
      }*/

    };
  });
